/* ---------------------------------------------------------------------- */
/* Script generated with: DeZign for Databases v6.0.0                     */
/* Target DBMS:           MS SQL Server 2005                              */
/* Project file:          official_db_scheme.dez                          */
/* Project name:                                                          */
/* Author:                                                                */
/* Script type:           Database creation script                        */
/* Created on:            2010-01-22 03:51                                */
/* ---------------------------------------------------------------------- */


/* ---------------------------------------------------------------------- */
/* Tables                                                                 */
/* ---------------------------------------------------------------------- */

/* ---------------------------------------------------------------------- */
/* Add table "[user]"                                                     */
/* ---------------------------------------------------------------------- */

CREATE TABLE [user] (
    [user_id] INTEGER IDENTITY(0,1) NOT NULL,
    [user_email] VARCHAR(64) NOT NULL,
    [user_password] VARCHAR(64) NOT NULL,
    [user_firstname] VARCHAR(64),
    [user_lastname] VARCHAR(64),
    [user_birthday] DATETIME,
    [user_sex] INTEGER CONSTRAINT [DEF_user_user_sex] DEFAULT 0,
    [user_phone] VARCHAR(32),
    [user_address] VARCHAR(256),
    CONSTRAINT [PK_user] PRIMARY KEY ([user_id])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "project"                                                    */
/* ---------------------------------------------------------------------- */

CREATE TABLE [project] (
    [project_id] INTEGER IDENTITY(0,1) NOT NULL,
    [manager_id] INTEGER NOT NULL,
    [project_name] VARCHAR(128) NOT NULL,
    [project_description] TEXT NOT NULL,
    [project_start_date] DATETIME NOT NULL,
    [project_end_date] DATETIME NOT NULL,
    CONSTRAINT [PK_project] PRIMARY KEY ([project_id])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "milestone"                                                  */
/* ---------------------------------------------------------------------- */

CREATE TABLE [milestone] (
    [milestone_id] INTEGER IDENTITY(0,1) NOT NULL,
    [project_id] INTEGER NOT NULL,
    [milestone_name] VARCHAR(128) NOT NULL,
    [milestone_description] TEXT,
    [milestone_start_date] DATETIME NOT NULL,
    [milestone_end_date] DATETIME NOT NULL,
    CONSTRAINT [PK_milestone] PRIMARY KEY ([milestone_id])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "work_on"                                                    */
/* ---------------------------------------------------------------------- */

CREATE TABLE [work_on] (
    [user_id] INTEGER NOT NULL,
    [project_id] INTEGER NOT NULL,
    [role] VARCHAR(16) CONSTRAINT [DEF_work_on_role] DEFAULT 'OBSERVER' NOT NULL,
    CONSTRAINT [PK_work_on] PRIMARY KEY ([user_id], [project_id])
)
GO

EXECUTE sp_addextendedproperty N'MS_Description', N'MANAGER | TEAMLEAD | DEVELOPER | TESTER | OBSERVER', 'SCHEMA', N'dbo', 'TABLE', N'work_on', 'COLUMN', N'role'
GO

/* ---------------------------------------------------------------------- */
/* Add table "phase"                                                      */
/* ---------------------------------------------------------------------- */

CREATE TABLE [phase] (
    [phase_id] INTEGER IDENTITY(0,1) NOT NULL,
    [milestone_id] INTEGER NOT NULL,
    [phase_name] VARCHAR(128) NOT NULL,
    [phase_description] TEXT,
    [phase_start_date] DATETIME NOT NULL,
    [phase_end_date] DATETIME NOT NULL,
    CONSTRAINT [PK_phase] PRIMARY KEY ([phase_id])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "task"                                                       */
/* ---------------------------------------------------------------------- */

CREATE TABLE [task] (
    [task_id] INTEGER IDENTITY(0,1) NOT NULL,
    [phase_id] INTEGER NOT NULL,
    [task_name] VARCHAR(128) NOT NULL,
    [task_description] TEXT,
    [task_start_date] DATETIME NOT NULL,
    [task_end_date] DATETIME NOT NULL,
    [task_report_progress] TEXT,
    [task_status] VARCHAR(8) CONSTRAINT [DEF_task_task_status] DEFAULT 'Warning' NOT NULL,
    CONSTRAINT [PK_task] PRIMARY KEY ([task_id])
)
GO

EXECUTE sp_addextendedproperty N'MS_Description', N'Warning | Bad | Good', 'SCHEMA', N'dbo', 'TABLE', N'task', 'COLUMN', N'task_status'
GO

/* ---------------------------------------------------------------------- */
/* Add table "assign"                                                     */
/* ---------------------------------------------------------------------- */

CREATE TABLE [assign] (
    [user_id] INTEGER NOT NULL,
    [task_id] INTEGER NOT NULL,
    CONSTRAINT [PK_assign] PRIMARY KEY ([user_id], [task_id])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "documentation"                                              */
/* ---------------------------------------------------------------------- */

CREATE TABLE [documentation] (
    [documentation_id] INTEGER IDENTITY(0,1) NOT NULL,
    [uploader_id] INTEGER NOT NULL,
    [folder_id] INTEGER NOT NULL,
    [documentation_name] VARCHAR(128) NOT NULL,
    [documentation_url] VARCHAR(512) NOT NULL,
    [documentation_date] DATETIME NOT NULL,
    [documentation_status] VARCHAR(16) CONSTRAINT [DEF_documentation_documentation_status] DEFAULT 'AVAILABLE' NOT NULL,
    CONSTRAINT [PK_documentation] PRIMARY KEY ([documentation_id])
)
GO

EXECUTE sp_addextendedproperty N'MS_Description', N'AVAILABLE | DELETED', 'SCHEMA', N'dbo', 'TABLE', N'documentation', 'COLUMN', N'documentation_status'
GO

/* ---------------------------------------------------------------------- */
/* Add table "folder"                                                     */
/* ---------------------------------------------------------------------- */

CREATE TABLE [folder] (
    [folder_id] INTEGER IDENTITY(0,1) NOT NULL,
    [project_id] INTEGER NOT NULL,
    [folder_name] VARCHAR(128) NOT NULL,
    CONSTRAINT [PK_folder] PRIMARY KEY ([folder_id])
)
GO

/* ---------------------------------------------------------------------- */
/* Foreign key constraints                                                */
/* ---------------------------------------------------------------------- */

ALTER TABLE [project] ADD CONSTRAINT [user_project] 
    FOREIGN KEY ([manager_id]) REFERENCES [user] ([user_id]) ON DELETE CASCADE ON UPDATE CASCADE
GO

ALTER TABLE [milestone] ADD CONSTRAINT [project_milestone] 
    FOREIGN KEY ([project_id]) REFERENCES [project] ([project_id]) ON DELETE CASCADE ON UPDATE CASCADE
GO

ALTER TABLE [work_on] ADD CONSTRAINT [user_work_on] 
    FOREIGN KEY ([user_id]) REFERENCES [user] ([user_id]) ON DELETE CASCADE ON UPDATE CASCADE
GO

ALTER TABLE [work_on] ADD CONSTRAINT [project_work_on] 
    FOREIGN KEY ([project_id]) REFERENCES [project] ([project_id])
GO

ALTER TABLE [phase] ADD CONSTRAINT [milestone_phase] 
    FOREIGN KEY ([milestone_id]) REFERENCES [milestone] ([milestone_id]) ON DELETE CASCADE ON UPDATE CASCADE
GO

ALTER TABLE [task] ADD CONSTRAINT [phase_task] 
    FOREIGN KEY ([phase_id]) REFERENCES [phase] ([phase_id]) ON DELETE CASCADE ON UPDATE CASCADE
GO

ALTER TABLE [assign] ADD CONSTRAINT [user_assign] 
    FOREIGN KEY ([user_id]) REFERENCES [user] ([user_id])
GO

ALTER TABLE [assign] ADD CONSTRAINT [task_assign] 
    FOREIGN KEY ([task_id]) REFERENCES [task] ([task_id]) ON DELETE CASCADE ON UPDATE CASCADE
GO

ALTER TABLE [documentation] ADD CONSTRAINT [user_documentation] 
    FOREIGN KEY ([uploader_id]) REFERENCES [user] ([user_id])
GO

ALTER TABLE [documentation] ADD CONSTRAINT [folder_documentation] 
    FOREIGN KEY ([folder_id]) REFERENCES [folder] ([folder_id]) ON DELETE CASCADE ON UPDATE CASCADE
GO

ALTER TABLE [folder] ADD CONSTRAINT [project_folder] 
    FOREIGN KEY ([project_id]) REFERENCES [project] ([project_id]) ON DELETE CASCADE ON UPDATE CASCADE
GO
S E T   A N S I _ N U L L S   O N  
 G O  
 S E T   Q U O T E D _ I D E N T I F I E R   O N  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ u s e r ] ' )   A N D   t y p e   i n   ( N ' U ' ) )  
 B E G I N  
 C R E A T E   T A B L E   [ d b o ] . [ u s e r ] (  
 	 [ u s e r _ i d ]   [ i n t ]   I D E N T I T Y ( 0 , 1 )   N O T   N U L L ,  
 	 [ u s e r _ e m a i l ]   [ v a r c h a r ] ( 6 4 )   N O T   N U L L ,  
 	 [ u s e r _ p a s s w o r d ]   [ v a r c h a r ] ( 6 4 )   N O T   N U L L ,  
 	 [ u s e r _ f i r s t n a m e ]   [ v a r c h a r ] ( 6 4 )   N U L L ,  
 	 [ u s e r _ l a s t n a m e ]   [ v a r c h a r ] ( 6 4 )   N U L L ,  
 	 [ u s e r _ b i r t h d a y ]   [ d a t e t i m e ]   N U L L ,  
 	 [ u s e r _ s e x ]   [ i n t ]   N U L L   C O N S T R A I N T   [ D E F _ u s e r _ u s e r _ s e x ]     D E F A U L T   ( ( 0 ) ) ,  
 	 [ u s e r _ p h o n e ]   [ v a r c h a r ] ( 3 2 )   N U L L ,  
 	 [ u s e r _ a d d r e s s ]   [ v a r c h a r ] ( 2 5 6 )   N U L L ,  
   C O N S T R A I N T   [ P K _ u s e r ]   P R I M A R Y   K E Y   C L U S T E R E D    
 (  
 	 [ u s e r _ i d ]   A S C  
 ) W I T H   ( P A D _ I N D E X     =   O F F ,   I G N O R E _ D U P _ K E Y   =   O F F )   O N   [ P R I M A R Y ]  
 )   O N   [ P R I M A R Y ]  
 E N D  
 G O  
 S E T   A N S I _ N U L L S   O N  
 G O  
 S E T   Q U O T E D _ I D E N T I F I E R   O N  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ p h a s e ] ' )   A N D   t y p e   i n   ( N ' U ' ) )  
 B E G I N  
 C R E A T E   T A B L E   [ d b o ] . [ p h a s e ] (  
 	 [ p h a s e _ i d ]   [ i n t ]   I D E N T I T Y ( 0 , 1 )   N O T   N U L L ,  
 	 [ m i l e s t o n e _ i d ]   [ i n t ]   N O T   N U L L ,  
 	 [ p h a s e _ n a m e ]   [ v a r c h a r ] ( 1 2 8 )   N O T   N U L L ,  
 	 [ p h a s e _ d e s c r i p t i o n ]   [ t e x t ]   N U L L ,  
 	 [ p h a s e _ s t a r t _ d a t e ]   [ d a t e t i m e ]   N O T   N U L L ,  
 	 [ p h a s e _ e n d _ d a t e ]   [ d a t e t i m e ]   N O T   N U L L ,  
   C O N S T R A I N T   [ P K _ p h a s e ]   P R I M A R Y   K E Y   C L U S T E R E D    
 (  
 	 [ p h a s e _ i d ]   A S C  
 ) W I T H   ( P A D _ I N D E X     =   O F F ,   I G N O R E _ D U P _ K E Y   =   O F F )   O N   [ P R I M A R Y ]  
 )   O N   [ P R I M A R Y ]   T E X T I M A G E _ O N   [ P R I M A R Y ]  
 E N D  
 G O  
 S E T   A N S I _ N U L L S   O N  
 G O  
 S E T   Q U O T E D _ I D E N T I F I E R   O N  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ t a s k ] ' )   A N D   t y p e   i n   ( N ' U ' ) )  
 B E G I N  
 C R E A T E   T A B L E   [ d b o ] . [ t a s k ] (  
 	 [ t a s k _ i d ]   [ i n t ]   I D E N T I T Y ( 0 , 1 )   N O T   N U L L ,  
 	 [ p h a s e _ i d ]   [ i n t ]   N O T   N U L L ,  
 	 [ t a s k _ n a m e ]   [ v a r c h a r ] ( 1 2 8 )   N O T   N U L L ,  
 	 [ t a s k _ d e s c r i p t i o n ]   [ t e x t ]   N U L L ,  
 	 [ t a s k _ s t a r t _ d a t e ]   [ d a t e t i m e ]   N O T   N U L L ,  
 	 [ t a s k _ e n d _ d a t e ]   [ d a t e t i m e ]   N O T   N U L L ,  
 	 [ t a s k _ r e p o r t _ p r o g r e s s ]   [ t e x t ]   N U L L ,  
 	 [ t a s k _ s t a t u s ]   [ v a r c h a r ] ( 8 )   N O T   N U L L   C O N S T R A I N T   [ D E F _ t a s k _ t a s k _ s t a t u s ]     D E F A U L T   ( ' W a r n i n g ' ) ,  
   C O N S T R A I N T   [ P K _ t a s k ]   P R I M A R Y   K E Y   C L U S T E R E D    
 (  
 	 [ t a s k _ i d ]   A S C  
 ) W I T H   ( P A D _ I N D E X     =   O F F ,   I G N O R E _ D U P _ K E Y   =   O F F )   O N   [ P R I M A R Y ]  
 )   O N   [ P R I M A R Y ]   T E X T I M A G E _ O N   [ P R I M A R Y ]  
 E N D  
 G O  
 S E T   A N S I _ N U L L S   O N  
 G O  
 S E T   Q U O T E D _ I D E N T I F I E R   O N  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ a s s i g n ] ' )   A N D   t y p e   i n   ( N ' U ' ) )  
 B E G I N  
 C R E A T E   T A B L E   [ d b o ] . [ a s s i g n ] (  
 	 [ u s e r _ i d ]   [ i n t ]   N O T   N U L L ,  
 	 [ t a s k _ i d ]   [ i n t ]   N O T   N U L L ,  
   C O N S T R A I N T   [ P K _ a s s i g n ]   P R I M A R Y   K E Y   C L U S T E R E D    
 (  
 	 [ u s e r _ i d ]   A S C ,  
 	 [ t a s k _ i d ]   A S C  
 ) W I T H   ( P A D _ I N D E X     =   O F F ,   I G N O R E _ D U P _ K E Y   =   O F F )   O N   [ P R I M A R Y ]  
 )   O N   [ P R I M A R Y ]  
 E N D  
 G O  
 S E T   A N S I _ N U L L S   O N  
 G O  
 S E T   Q U O T E D _ I D E N T I F I E R   O N  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ d o c u m e n t a t i o n ] ' )   A N D   t y p e   i n   ( N ' U ' ) )  
 B E G I N  
 C R E A T E   T A B L E   [ d b o ] . [ d o c u m e n t a t i o n ] (  
 	 [ d o c u m e n t a t i o n _ i d ]   [ i n t ]   I D E N T I T Y ( 0 , 1 )   N O T   N U L L ,  
 	 [ u p l o a d e r _ i d ]   [ i n t ]   N O T   N U L L ,  
 	 [ f o l d e r _ i d ]   [ i n t ]   N O T   N U L L ,  
 	 [ d o c u m e n t a t i o n _ n a m e ]   [ v a r c h a r ] ( 1 2 8 )   N O T   N U L L ,  
 	 [ d o c u m e n t a t i o n _ u r l ]   [ v a r c h a r ] ( 5 1 2 )   N O T   N U L L ,  
 	 [ d o c u m e n t a t i o n _ d a t e ]   [ d a t e t i m e ]   N O T   N U L L ,  
 	 [ d o c u m e n t a t i o n _ s t a t u s ]   [ v a r c h a r ] ( 1 6 )   N O T   N U L L   C O N S T R A I N T   [ D E F _ d o c u m e n t a t i o n _ d o c u m e n t a t i o n _ s t a t u s ]     D E F A U L T   ( ' A V A I L A B L E ' ) ,  
   C O N S T R A I N T   [ P K _ d o c u m e n t a t i o n ]   P R I M A R Y   K E Y   C L U S T E R E D    
 (  
 	 [ d o c u m e n t a t i o n _ i d ]   A S C  
 ) W I T H   ( P A D _ I N D E X     =   O F F ,   I G N O R E _ D U P _ K E Y   =   O F F )   O N   [ P R I M A R Y ]  
 )   O N   [ P R I M A R Y ]  
 E N D  
 G O  
 S E T   A N S I _ N U L L S   O N  
 G O  
 S E T   Q U O T E D _ I D E N T I F I E R   O N  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ w o r k _ o n ] ' )   A N D   t y p e   i n   ( N ' U ' ) )  
 B E G I N  
 C R E A T E   T A B L E   [ d b o ] . [ w o r k _ o n ] (  
 	 [ u s e r _ i d ]   [ i n t ]   N O T   N U L L ,  
 	 [ p r o j e c t _ i d ]   [ i n t ]   N O T   N U L L ,  
 	 [ r o l e ]   [ v a r c h a r ] ( 1 6 )   N O T   N U L L   C O N S T R A I N T   [ D E F _ w o r k _ o n _ r o l e ]     D E F A U L T   ( ' O B S E R V E R ' ) ,  
   C O N S T R A I N T   [ P K _ w o r k _ o n ]   P R I M A R Y   K E Y   C L U S T E R E D    
 (  
 	 [ u s e r _ i d ]   A S C ,  
 	 [ p r o j e c t _ i d ]   A S C  
 ) W I T H   ( P A D _ I N D E X     =   O F F ,   I G N O R E _ D U P _ K E Y   =   O F F )   O N   [ P R I M A R Y ]  
 )   O N   [ P R I M A R Y ]  
 E N D  
 G O  
 S E T   A N S I _ N U L L S   O N  
 G O  
 S E T   Q U O T E D _ I D E N T I F I E R   O N  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ p r o j e c t ] ' )   A N D   t y p e   i n   ( N ' U ' ) )  
 B E G I N  
 C R E A T E   T A B L E   [ d b o ] . [ p r o j e c t ] (  
 	 [ p r o j e c t _ i d ]   [ i n t ]   I D E N T I T Y ( 0 , 1 )   N O T   N U L L ,  
 	 [ m a n a g e r _ i d ]   [ i n t ]   N O T   N U L L ,  
 	 [ p r o j e c t _ n a m e ]   [ v a r c h a r ] ( 1 2 8 )   N O T   N U L L ,  
 	 [ p r o j e c t _ d e s c r i p t i o n ]   [ t e x t ]   N O T   N U L L ,  
 	 [ p r o j e c t _ s t a r t _ d a t e ]   [ d a t e t i m e ]   N O T   N U L L ,  
 	 [ p r o j e c t _ e n d _ d a t e ]   [ d a t e t i m e ]   N O T   N U L L ,  
   C O N S T R A I N T   [ P K _ p r o j e c t ]   P R I M A R Y   K E Y   C L U S T E R E D    
 (  
 	 [ p r o j e c t _ i d ]   A S C  
 ) W I T H   ( P A D _ I N D E X     =   O F F ,   I G N O R E _ D U P _ K E Y   =   O F F )   O N   [ P R I M A R Y ]  
 )   O N   [ P R I M A R Y ]   T E X T I M A G E _ O N   [ P R I M A R Y ]  
 E N D  
 G O  
 S E T   A N S I _ N U L L S   O N  
 G O  
 S E T   Q U O T E D _ I D E N T I F I E R   O N  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ m i l e s t o n e ] ' )   A N D   t y p e   i n   ( N ' U ' ) )  
 B E G I N  
 C R E A T E   T A B L E   [ d b o ] . [ m i l e s t o n e ] (  
 	 [ m i l e s t o n e _ i d ]   [ i n t ]   I D E N T I T Y ( 0 , 1 )   N O T   N U L L ,  
 	 [ p r o j e c t _ i d ]   [ i n t ]   N O T   N U L L ,  
 	 [ m i l e s t o n e _ n a m e ]   [ v a r c h a r ] ( 1 2 8 )   N O T   N U L L ,  
 	 [ m i l e s t o n e _ d e s c r i p t i o n ]   [ t e x t ]   N U L L ,  
 	 [ m i l e s t o n e _ s t a r t _ d a t e ]   [ d a t e t i m e ]   N O T   N U L L ,  
 	 [ m i l e s t o n e _ e n d _ d a t e ]   [ d a t e t i m e ]   N O T   N U L L ,  
   C O N S T R A I N T   [ P K _ m i l e s t o n e ]   P R I M A R Y   K E Y   C L U S T E R E D    
 (  
 	 [ m i l e s t o n e _ i d ]   A S C  
 ) W I T H   ( P A D _ I N D E X     =   O F F ,   I G N O R E _ D U P _ K E Y   =   O F F )   O N   [ P R I M A R Y ]  
 )   O N   [ P R I M A R Y ]   T E X T I M A G E _ O N   [ P R I M A R Y ]  
 E N D  
 G O  
 S E T   A N S I _ N U L L S   O N  
 G O  
 S E T   Q U O T E D _ I D E N T I F I E R   O N  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . o b j e c t s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ f o l d e r ] ' )   A N D   t y p e   i n   ( N ' U ' ) )  
 B E G I N  
 C R E A T E   T A B L E   [ d b o ] . [ f o l d e r ] (  
 	 [ f o l d e r _ i d ]   [ i n t ]   I D E N T I T Y ( 0 , 1 )   N O T   N U L L ,  
 	 [ p r o j e c t _ i d ]   [ i n t ]   N O T   N U L L ,  
 	 [ f o l d e r _ n a m e ]   [ v a r c h a r ] ( 1 2 8 )   N O T   N U L L ,  
   C O N S T R A I N T   [ P K _ f o l d e r ]   P R I M A R Y   K E Y   C L U S T E R E D    
 (  
 	 [ f o l d e r _ i d ]   A S C  
 ) W I T H   ( P A D _ I N D E X     =   O F F ,   I G N O R E _ D U P _ K E Y   =   O F F )   O N   [ P R I M A R Y ]  
 )   O N   [ P R I M A R Y ]  
 E N D  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . f o r e i g n _ k e y s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ m i l e s t o n e _ p h a s e ] ' )   A N D   p a r e n t _ o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ p h a s e ] ' ) )  
 A L T E R   T A B L E   [ d b o ] . [ p h a s e ]     W I T H   C H E C K   A D D     C O N S T R A I N T   [ m i l e s t o n e _ p h a s e ]   F O R E I G N   K E Y ( [ m i l e s t o n e _ i d ] )  
 R E F E R E N C E S   [ d b o ] . [ m i l e s t o n e ]   ( [ m i l e s t o n e _ i d ] )  
 O N   U P D A T E   C A S C A D E  
 O N   D E L E T E   C A S C A D E  
 G O  
 A L T E R   T A B L E   [ d b o ] . [ p h a s e ]   C H E C K   C O N S T R A I N T   [ m i l e s t o n e _ p h a s e ]  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . f o r e i g n _ k e y s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ p h a s e _ t a s k ] ' )   A N D   p a r e n t _ o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ t a s k ] ' ) )  
 A L T E R   T A B L E   [ d b o ] . [ t a s k ]     W I T H   C H E C K   A D D     C O N S T R A I N T   [ p h a s e _ t a s k ]   F O R E I G N   K E Y ( [ p h a s e _ i d ] )  
 R E F E R E N C E S   [ d b o ] . [ p h a s e ]   ( [ p h a s e _ i d ] )  
 O N   U P D A T E   C A S C A D E  
 O N   D E L E T E   C A S C A D E  
 G O  
 A L T E R   T A B L E   [ d b o ] . [ t a s k ]   C H E C K   C O N S T R A I N T   [ p h a s e _ t a s k ]  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . f o r e i g n _ k e y s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ t a s k _ a s s i g n ] ' )   A N D   p a r e n t _ o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ a s s i g n ] ' ) )  
 A L T E R   T A B L E   [ d b o ] . [ a s s i g n ]     W I T H   C H E C K   A D D     C O N S T R A I N T   [ t a s k _ a s s i g n ]   F O R E I G N   K E Y ( [ t a s k _ i d ] )  
 R E F E R E N C E S   [ d b o ] . [ t a s k ]   ( [ t a s k _ i d ] )  
 O N   U P D A T E   C A S C A D E  
 O N   D E L E T E   C A S C A D E  
 G O  
 A L T E R   T A B L E   [ d b o ] . [ a s s i g n ]   C H E C K   C O N S T R A I N T   [ t a s k _ a s s i g n ]  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . f o r e i g n _ k e y s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ u s e r _ a s s i g n ] ' )   A N D   p a r e n t _ o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ a s s i g n ] ' ) )  
 A L T E R   T A B L E   [ d b o ] . [ a s s i g n ]     W I T H   C H E C K   A D D     C O N S T R A I N T   [ u s e r _ a s s i g n ]   F O R E I G N   K E Y ( [ u s e r _ i d ] )  
 R E F E R E N C E S   [ d b o ] . [ u s e r ]   ( [ u s e r _ i d ] )  
 G O  
 A L T E R   T A B L E   [ d b o ] . [ a s s i g n ]   C H E C K   C O N S T R A I N T   [ u s e r _ a s s i g n ]  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . f o r e i g n _ k e y s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ f o l d e r _ d o c u m e n t a t i o n ] ' )   A N D   p a r e n t _ o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ d o c u m e n t a t i o n ] ' ) )  
 A L T E R   T A B L E   [ d b o ] . [ d o c u m e n t a t i o n ]     W I T H   C H E C K   A D D     C O N S T R A I N T   [ f o l d e r _ d o c u m e n t a t i o n ]   F O R E I G N   K E Y ( [ f o l d e r _ i d ] )  
 R E F E R E N C E S   [ d b o ] . [ f o l d e r ]   ( [ f o l d e r _ i d ] )  
 O N   U P D A T E   C A S C A D E  
 O N   D E L E T E   C A S C A D E  
 G O  
 A L T E R   T A B L E   [ d b o ] . [ d o c u m e n t a t i o n ]   C H E C K   C O N S T R A I N T   [ f o l d e r _ d o c u m e n t a t i o n ]  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . f o r e i g n _ k e y s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ u s e r _ d o c u m e n t a t i o n ] ' )   A N D   p a r e n t _ o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ d o c u m e n t a t i o n ] ' ) )  
 A L T E R   T A B L E   [ d b o ] . [ d o c u m e n t a t i o n ]     W I T H   C H E C K   A D D     C O N S T R A I N T   [ u s e r _ d o c u m e n t a t i o n ]   F O R E I G N   K E Y ( [ u p l o a d e r _ i d ] )  
 R E F E R E N C E S   [ d b o ] . [ u s e r ]   ( [ u s e r _ i d ] )  
 G O  
 A L T E R   T A B L E   [ d b o ] . [ d o c u m e n t a t i o n ]   C H E C K   C O N S T R A I N T   [ u s e r _ d o c u m e n t a t i o n ]  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . f o r e i g n _ k e y s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ p r o j e c t _ w o r k _ o n ] ' )   A N D   p a r e n t _ o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ w o r k _ o n ] ' ) )  
 A L T E R   T A B L E   [ d b o ] . [ w o r k _ o n ]     W I T H   C H E C K   A D D     C O N S T R A I N T   [ p r o j e c t _ w o r k _ o n ]   F O R E I G N   K E Y ( [ p r o j e c t _ i d ] )  
 R E F E R E N C E S   [ d b o ] . [ p r o j e c t ]   ( [ p r o j e c t _ i d ] )  
 G O  
 A L T E R   T A B L E   [ d b o ] . [ w o r k _ o n ]   C H E C K   C O N S T R A I N T   [ p r o j e c t _ w o r k _ o n ]  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . f o r e i g n _ k e y s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ u s e r _ w o r k _ o n ] ' )   A N D   p a r e n t _ o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ w o r k _ o n ] ' ) )  
 A L T E R   T A B L E   [ d b o ] . [ w o r k _ o n ]     W I T H   C H E C K   A D D     C O N S T R A I N T   [ u s e r _ w o r k _ o n ]   F O R E I G N   K E Y ( [ u s e r _ i d ] )  
 R E F E R E N C E S   [ d b o ] . [ u s e r ]   ( [ u s e r _ i d ] )  
 O N   U P D A T E   C A S C A D E  
 O N   D E L E T E   C A S C A D E  
 G O  
 A L T E R   T A B L E   [ d b o ] . [ w o r k _ o n ]   C H E C K   C O N S T R A I N T   [ u s e r _ w o r k _ o n ]  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . f o r e i g n _ k e y s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ u s e r _ p r o j e c t ] ' )   A N D   p a r e n t _ o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ p r o j e c t ] ' ) )  
 A L T E R   T A B L E   [ d b o ] . [ p r o j e c t ]     W I T H   C H E C K   A D D     C O N S T R A I N T   [ u s e r _ p r o j e c t ]   F O R E I G N   K E Y ( [ m a n a g e r _ i d ] )  
 R E F E R E N C E S   [ d b o ] . [ u s e r ]   ( [ u s e r _ i d ] )  
 O N   U P D A T E   C A S C A D E  
 O N   D E L E T E   C A S C A D E  
 G O  
 A L T E R   T A B L E   [ d b o ] . [ p r o j e c t ]   C H E C K   C O N S T R A I N T   [ u s e r _ p r o j e c t ]  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . f o r e i g n _ k e y s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ p r o j e c t _ m i l e s t o n e ] ' )   A N D   p a r e n t _ o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ m i l e s t o n e ] ' ) )  
 A L T E R   T A B L E   [ d b o ] . [ m i l e s t o n e ]     W I T H   C H E C K   A D D     C O N S T R A I N T   [ p r o j e c t _ m i l e s t o n e ]   F O R E I G N   K E Y ( [ p r o j e c t _ i d ] )  
 R E F E R E N C E S   [ d b o ] . [ p r o j e c t ]   ( [ p r o j e c t _ i d ] )  
 O N   U P D A T E   C A S C A D E  
 O N   D E L E T E   C A S C A D E  
 G O  
 A L T E R   T A B L E   [ d b o ] . [ m i l e s t o n e ]   C H E C K   C O N S T R A I N T   [ p r o j e c t _ m i l e s t o n e ]  
 G O  
 I F   N O T   E X I S T S   ( S E L E C T   *   F R O M   s y s . f o r e i g n _ k e y s   W H E R E   o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ p r o j e c t _ f o l d e r ] ' )   A N D   p a r e n t _ o b j e c t _ i d   =   O B J E C T _ I D ( N ' [ d b o ] . [ f o l d e r ] ' ) )  
 A L T E R   T A B L E   [ d b o ] . [ f o l d e r ]     W I T H   C H E C K   A D D     C O N S T R A I N T   [ p r o j e c t _ f o l d e r ]   F O R E I G N   K E Y ( [ p r o j e c t _ i d ] )  
 R E F E R E N C E S   [ d b o ] . [ p r o j e c t ]   ( [ p r o j e c t _ i d ] )  
 O N   U P D A T E   C A S C A D E  
 O N   D E L E T E   C A S C A D E  
 G O  
 A L T E R   T A B L E   [ d b o ] . [ f o l d e r ]   C H E C K   C O N S T R A I N T   [ p r o j e c t _ f o l d e r ]  
 